<?php
namespace app\common\customer\logic;


use app\common\BaseLogic;
use app\common\customer\model\CustomerContact;

class ContactLogic extends BaseLogic
{
    public function getContactDefault($id, $fields="*") {
        $cacheName = "member:contact:{$id}";

        if ($fields == "*") {
            $data = $this->redis->hGetAll($cacheName);
        } else {
            $data = $this->redis->hMGet($cacheName, $fields);
        }

        if (empty($data['id'])) {
            $where = [];
            $where['member_id'] = $id;
            // $where['type'] = 1;
            $model = new CustomerContact();
            $row = $model->where($where)->find();
            if (!empty($row)) {
                $row->append(['role_type', 'tag']);
                $data = $row->getData();
                $data['role_type'] = $row['role_type'];
                $data['tag'] = $row['tag'];
                $this->redis->hMSet($cacheName, $data);
                if ($fields != "*") $data = array_search_keys($data, $fields);
            } else {
                $data = [];
            }
        }

        return $data;
    }

    public function resetCache($id){
        $cacheName = "member:contact:{$id}";
        $this->redis->del($cacheName);
    }
}